/**
 *
 * \file CXServerConnection.cpp
 * \brief This file contains the CXServerConnection class implementation.
 * \author Vladimir Klubkov (aka marked)
 *
 * Last changes date:
 * \date 15.03.2011
 *
 * Copyright 2011 Vladimir Klubkov <marked-one@yandex.ru>
 *
 * This file is part of OOXlib.
 *
 * OOXLib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * OOXlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OOXlib; if not, you can try to find  it here:
 * http://www.gnu.org/licenses/gpl.txt
 * or write to the Free Software Foundation, Inc., 59 Temple Place,
 * Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "../include/CXServerConnection.h"

namespace OOXlib {

/**
 * Constructor. Calls the base-class constructor.
 * \param File Source code file name.
 * \param iLine Source code line.
 * \param Function Function name.
 */
CXServerConnection::CNULLXOpenDisplayException::CNULLXOpenDisplayException(const ::std::string &File, const int iLine, const ::std::string &Function)
: CException(File, iLine, Function, "Can't establish the X Server connection using the NULL value as the XOpenDisplay function parameter. The XOpenDisplay function failed.") {}
/**
 * Constructor. Calls the base-class constructor.
 * \param File Source code file name.
 * \param iLine Source code line.
 * \param Function Function name.
 */
CXServerConnection::CGetenvXOpenDisplayException::CGetenvXOpenDisplayException(const ::std::string &File, const int iLine, const ::std::string &Function)
: CException(File, iLine, Function, "Can't establish the X Server connection using the ::getenv(\"DISPLAY\") value as the XOpenDisplay function parameter. The XOpenDisplay function failed.") {}
/**
 * Constructor. Calls the base-class constructor.
 * \param File Source code file name.
 * \param iLine Source code line.
 * \param Function Function name.
 */
CXServerConnection::CXOpenDisplayException::CXOpenDisplayException(const ::std::string &File, const int iLine, const ::std::string &Function)
: CException(File, iLine, Function, "Can't establish the X Server connection. Maybe the wrong display name was specified. The XOpenDisplay function failed.") {}
/**
 * Constructor. Opens the connection to the X Server and obtains screen count and the default screen number from it.
 */
CXServerConnection::CXServerConnection()
: m_pDisplay(NULL),  m_iScreensCount(0), m_iDefaultScreenNumber(0) {
    // For systems, which are not POSIX-conformant, we try to manually obtain the value of the "DISPLAY" environment variable.
    const char *dispEnv = ::getenv("DISPLAY");
    if(NULL == dispEnv) {
        m_pDisplay = ::XOpenDisplay(NULL);
        if(NULL == m_pDisplay) {
            OOXLib_THROW(CNULLXOpenDisplayException);
        }
    } else {
        m_pDisplay = ::XOpenDisplay(dispEnv);
        if(NULL == m_pDisplay) {
            OOXLib_THROW(CGetenvXOpenDisplayException);
        }
    }
    // So, if there are no screens available, it's not an error of the CXServerConnection class, that's why we don't need to check.
    m_iScreensCount = ::XScreenCount(m_pDisplay);
}
/**
 * Constructor. Opens the connection to the X Server and obtains screen count and the default screen number from it.
 * \param DisplayName std::string contatining the display name. Might also be empty.
 */
CXServerConnection::CXServerConnection(const ::std::string &DisplayName)
: m_pDisplay(NULL),  m_iScreensCount(0), m_iDefaultScreenNumber(0) {
    if(DisplayName.empty()) {
        // For systems, which are not POSIX-conformant, we try to manually obtain the value of the "DISPLAY" environment variable.
        const char *dispEnv = ::getenv("DISPLAY");
        if(NULL == dispEnv) {
            m_pDisplay = ::XOpenDisplay(NULL);
            if(NULL == m_pDisplay) {
                OOXLib_THROW(CNULLXOpenDisplayException);
            }
        } else {
            m_pDisplay = ::XOpenDisplay(dispEnv);
            if(NULL == m_pDisplay) {
                OOXLib_THROW(CGetenvXOpenDisplayException);
            }
        }
    } else {
        m_pDisplay = ::XOpenDisplay(DisplayName.c_str());
        if(NULL == m_pDisplay) {
            OOXLib_THROW(CXOpenDisplayException);
        }
    }
    // So, if there are no screens available, it's not an error of CXServerConnection class, that's why we don't need to check.
    m_iScreensCount = ::XScreenCount(m_pDisplay);
}
/**
 * Destructor. Calls the XCloseDisplay fucntion, which closes the connection to the X Server.
 */
CXServerConnection::~CXServerConnection() {
    // XCloseDisplay can generate a BadGC error.
    ::XCloseDisplay(m_pDisplay);
}
/**
 * Returns a pointer to the display structure.
 * \return A pointer to the display structure.
 */
Display* CXServerConnection::Get() const {
    return m_pDisplay;
}
/**
 * Returns a pointer to the display structure.
 * \return A pointer to the display structure.
 */
CXServerConnection::operator Display*() const {
    return m_pDisplay;
}
/**
 * Returns the screens count.
 * \return Screens count.
 */
int CXServerConnection::GetScreensCount() const {
    return m_iScreensCount;
}
/**
 * Checks the screen number for validity. To be valid, it must be lower than the screens count and greater or equal than nil.
 * \param iScreenNumber Number of the screen to check.
 * \return True, if the number is valid, else - false.
 */
bool CXServerConnection::IsScreenNumberValid(const int iScreenNumber) const {
// \todo (marked#1#): Is this valid? Haven't found any description how to check the screen number for validity.
    if(0 == m_iScreensCount) {
        return false;
    }
    if(0 > iScreenNumber) {
        return false;
    }
    if(iScreenNumber >= m_iScreensCount) {
        return false;
    }
    return true;
}
/**
 * Returns the string that was passed to the constructor of the CXServerConnection class, or the default value (DISPLAY environment variable value for POSIX-conformant systems).
 * \return The string that was passed to the constructor.
 */
::std::string CXServerConnection::ServerName() const {
    return ::XDisplayString(m_pDisplay);
}
/**
 * Returns the server vendor name.
 * \return A null terminated string containing the server vendor name.
 */
::std::string CXServerConnection::ServerVendorName() const {
    return ::XServerVendor(m_pDisplay);
}
/**
 * Returns the server vendor release number.
 * \return Server vendor release number.
 */
int CXServerConnection::ServerVendorRelease() const {
    return ::XVendorRelease(m_pDisplay);
}
/**
 * Returns the major version number (11) of the X protocol associated with the connected display.
 * \return The major version number (11) of the X protocol.
 */
int CXServerConnection::ProtocolVersion() const {
    return ::XProtocolVersion(m_pDisplay);
}
/**
 * Returns the minor protocol revision number of the X protocol associated with the connected display.
 * \return The minor protocol revision number of the X server.
 */
int CXServerConnection::ProtocolRevision() const {
    return ::XProtocolRevision(m_pDisplay);
}
/**
 * Returns the connection number (on a POSIX-conformant system, this is the file descriptor of the connection).
 * \return Connection number.
 */
int CXServerConnection::ConnectionNumber() const {
    return ::XConnectionNumber(m_pDisplay);
}
/**
 * Constructor. Calls the base-class constructor.
 * \param File Source code file name.
 * \param iLine Source code line.
 * \param Function Function name.
 */
CNULLPtrConnectionException::CNULLPtrConnectionException(const ::std::string &File, const int iLine, const ::std::string &Function)
: CException(File, iLine, Function, "NULL was specified as a pointer to the CXServerConnection object.") {}

} // OOXlib
